package com.zhao.dota.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.dota.bo.PlayerMatchSummary;
import com.zhao.dota.bo.PlayerPeers;
import com.zhao.dota.model.MatchPlayer;
import com.zhao.dota.vo.HeroWinRate;
import org.apache.ibatis.annotations.Select;
import org.apache.tomcat.jni.Local;
import org.springframework.stereotype.Repository;

import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface MatchPlayerMapper extends BaseMapper<MatchPlayer> {

    @Select("<script>" +
            "SELECT " +
            " sum( win ) as win, " +
            " count( id ) as matchId " +
            "  FROM match_player " +
            " WHERE steam_id = #{steamId} " +
            " ORDER BY end_time desc" +
            "<if test='limit > 0'>" +
            " limit #{limit}" +
            "</if>" +
            "</script>")
    MatchPlayer countWinRate(Long steamId, int limit);

    @Select("SELECT " +
            " a.end_time, " +
            " a.match_id, " +
            " b.hero_name, " +
            " b.win, " +
            " a.game_mode, " +
            " a.playback_time, " +
            " a.game_time, " +
            " c.kills, " +
            " c.deaths, " +
            " c.assists  " +
            "  FROM `dota_match` a" +
            "  LEFT JOIN match_player b ON a.id = b.match_id " +
            "  LEFT JOIN performance c ON b.id = c.match_player_id  " +
            " WHERE b.steam_id = #{steamId} " +
            " ORDER BY " +
            " a.end_time DESC")
    Page<PlayerMatchSummary> page(Page page, Long steamId);

    @Select("<script>" +
            "select c.*,d.count,d.wins from player c " +
            "  left join  " +
            "(SELECT " +
            " count(a.id) AS `count`, " +
            " a.steam_id, " +
            " sum(b.win) wins " +
            "   FROM match_player a " +
            "  INNER JOIN match_player b ON a.match_id = b.match_id  " +
            "<if test='peer'>" +
            "    AND a.team = b.team  " +
            "</if>" +
            "<if test='!peer'>" +
            "    AND a.team != b.team  " +
            "</if>" +
            "  WHERE a.steam_id != b.steam_id " +
            "    AND b.steam_id = #{steamId}  " +
            "  GROUP BY a.steam_id ) d " +
            "    on c.steam_id = d.steam_id " +
            " where d.steam_id is not null " +
            " order by d.count desc " +
            "</script>")
    Page<PlayerPeers> peersOrEnemies(Page page, Long steamId, boolean peer);

    @Select("SELECT g.*, h.NAME AS heroName, h.localized_name AS heroLocalizedName " +
            "  FROM ( " +
            "   SELECT hero_id, player_id, COUNT(id) AS totals, SUM(win) AS wins, SUM(kills) AS kills " +
            "   , SUM(deaths) AS deaths, SUM(assists) AS assists " +
            "     FROM ( " +
            "      SELECT mp.hero_id, mp.player_id, mp.win, mp.id, p.kills, p.assists, p.deaths " +
            "        FROM match_player mp " +
            "        LEFT JOIN performance p ON mp.id = p.match_player_id" +
            "       WHERE mp.player_id = #{playerId} " +
            "     ) a " +
            "    GROUP BY hero_id, player_id) g" +
            "  LEFT JOIN hero h ON g.hero_id = h.id " +
            " ORDER BY totals DESC,wins DESC")
    Page<HeroWinRate> groupByPlayerHero(Page page, Integer playerId);

    @Select("<script>" +
            "SELECT g.*, h.NAME AS heroName, h.localized_name AS heroLocalizedName " +
            "  FROM ( " +
            "   SELECT hero_id, COUNT(id) AS totals, SUM(win) AS wins, SUM(kills) AS kills, SUM(deaths) AS deaths, SUM(assists) AS assists " +
            "     FROM ( " +
            "      SELECT mp.hero_id, mp.win, mp.id, p.kills, p.assists, p.deaths " +
            "        FROM match_player mp " +
            "<if test='startTime != null or endTime != null'>" +
            "        LEFT JOIN dota_match d ON mp.match_id = d.id " +
            "</if>" +
            "        LEFT JOIN performance p ON mp.id = p.match_player_id " +
            "       WHERE 1 = 1 " +
            "<if test='startTime != null'>" +
            "         AND d.end_time >= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "         AND d.end_time &lt;= #{endTime} " +
            "</if>" +
            "     ) a " +
            "    GROUP BY hero_id " +
            "  ) g " +
            "  LEFT JOIN hero h ON g.hero_id = h.id " +
            " ORDER BY totals DESC,wins DESC" +
            "</script>")
    Page<HeroWinRate> groupByHero(Page page, LocalDateTime startTime, LocalDateTime endTime);

}
